package com.group.project.hrms.controller;

import com.alibaba.fastjson.JSON;
import com.group.project.hrms.entity.ContactBasic;
import com.group.project.hrms.entity.FrontLineSupervisorDepts;
import com.group.project.hrms.entity.ResultEntity.ResultEntity;
import com.group.project.hrms.entity.SystemAuthenticationList;
import com.group.project.hrms.service.*;
import com.group.project.hrms.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
public class FrontLineSupervisorDeptsController {

    @Autowired
    private IFrontLineSupervisorDeptsService iFrontLineSupervisorDeptsService;

    @Autowired
    private IContactBasicService iContactBasicService;

    @Autowired
    private IContactBasicDeptInfoService iContactBasicDeptInfoService;

    @Autowired
    private ISystemAuthenticationListService iSystemAuthenticationListService;

    @Autowired
    private ISystemAuthorityListService iSystemAuthorityListService;

    @Autowired
    private LogUtil logUtil;


    /**
     * 该方法加载系统设置页面的初始化数据
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/getSystemSettingsPageDates")
    @ResponseBody
    public ResultEntity getSystemSettingsPageDates(HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        List<Map> list = new ArrayList<>();
        Map<String, List> map = new HashMap<>();
        try{
            map.put("contactList", iContactBasicService.selectAllContactBasicByStaffStatus(1));//1表示在职人员
            map.put("deptList", iContactBasicDeptInfoService.selectAllContactBasicDeptInfo()); //部门列表
            map.put("supervisorList", iFrontLineSupervisorDeptsService.selectAllFrontLineSupervisorDepts()); //生产主管管理范围清单
            map.put("authenticationList", iSystemAuthenticationListService.selectAllSystemAuthenticationList()); //系统用户及对应的权限清单
            map.put("authorityList", iSystemAuthorityListService.selectAllSystemAuthorityList()); //系统内置的权限清单
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("F");
            resultEntity.setMsg("请求数据失败");
            resultEntity.setDataList(list);
            return resultEntity;
        }
        list.add(map);
        resultEntity.setCode("S");
        resultEntity.setMsg("Success");
        resultEntity.setDataList(list);
        return resultEntity;
    }

    /**
     * 该方法接收前端传入的管理范围对象数据，向数据库做持久化操作
     * @param map 前端传入的管理范围对象数据
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/insertFrontLineSupervisorDepts")
    @ResponseBody
    public ResultEntity insertFrontLineSupervisorDepts(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Object staffName = request.getAttribute("staffName");
        Date date = new Date();
        Object supervisorid = map.get("supervisorid"); //主管人员ID
        Object deptid = map.get("deptid"); //所管辖的部门列表 deptid=[379157317, 379101601, 371942968]
        if(null != supervisorid && null != deptid){
            //插入数据前，需要校验该人员是否已经存在
            FrontLineSupervisorDepts frontLineSupervisorDepts = iFrontLineSupervisorDeptsService.selectFrontLineSupervisorDeptsBySID(supervisorid.toString());
            if(null != frontLineSupervisorDepts){
                resultEntity.setCode("F");
                resultEntity.setMsg("该主管人员已经存在，请通过编辑功能更改其管理范围");
                return resultEntity;
            }
            int count = 0;
            //查询该主管人员是否有使用一线生产管理系统的登录权限
            SystemAuthenticationList authenticationList = iSystemAuthenticationListService.selectSystemAuthenticationListByContactId(supervisorid.toString());
            if(null == authenticationList){
                resultEntity.setCode("F");
                resultEntity.setMsg("该主管人员没有访问一线主管管理系统的权限，请先为其添加权限，再添加管理范围");
                return resultEntity;
            }else {
                String authIDs = authenticationList.getAuthIds();
                if(!authIDs.contains("auth0001")){
                    resultEntity.setCode("F");
                    resultEntity.setMsg("该主管人员没有访问一线主管管理系统的权限，请先为其添加权限，再添加管理范围");
                    return resultEntity;
                }
            }
            //创建管理范围对象
            FrontLineSupervisorDepts targetObject = new FrontLineSupervisorDepts();
            targetObject.setcTime(date);
            targetObject.setSupervisorContactId(supervisorid.toString());
            targetObject.setDeptIds(JSON.toJSONString(deptid));
            count = iFrontLineSupervisorDeptsService.insertFrontLineSupervisorDepts(targetObject);
            if(count != 0){
                ContactBasic contactBasic = iContactBasicService.selectContactBasicByContactID(supervisorid.toString());
                logUtil.insertSystemEventLog(staffName.toString(), date, "设置了" + contactBasic.getStaffName() + "的一线主管管理范围", "系统");
                resultEntity.setCode("S");
                resultEntity.setMsg("创建成功");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("创建失败");
        return resultEntity;
    }

    /**
     * 该方法接收前端传入的更细管理范围对象数据，向数据库做更新操作
     * @param map 需要更新的数据对象
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/updateFrontLineSupervisorDepts")
    @ResponseBody
    public ResultEntity updateFrontLineSupervisorDepts(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Object staffName = request.getAttribute("staffName");
        Date date = new Date();
        Object supervisorid = map.get("supervisorid");
        Object deptid = map.get("deptid");
        if(null != supervisorid && null != deptid){
            try{
                FrontLineSupervisorDepts frontLineSupervisorDepts = iFrontLineSupervisorDeptsService.selectFrontLineSupervisorDeptsBySID(supervisorid.toString());
                frontLineSupervisorDepts.setDeptIds(JSON.toJSONString(deptid));
                frontLineSupervisorDepts.setmTime(date);
                int count = iFrontLineSupervisorDeptsService.updateFrontLineSupervisorDepts(frontLineSupervisorDepts);
                if(count != 0){
                    ContactBasic contactBasic = iContactBasicService.selectContactBasicByContactID(supervisorid.toString());
                    logUtil.insertSystemEventLog(staffName.toString(), date, "更新了" + contactBasic.getStaffName() + "的一线主管管理范围", "系统");
                    resultEntity.setCode("S");
                    resultEntity.setMsg("更新成功");
                    return resultEntity;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("更新失败");
        return resultEntity;
    }

    /**
     * 该方法接收前端用户传入的删除对象指令，向数据库删除一个管理范围对象
     * @param map 删除对象
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/deleteFrontLineSupervisorDepts")
    @ResponseBody
    public ResultEntity deleteFrontLineSupervisorDepts(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Object staffName = request.getAttribute("staffName");
        Object supervisorContactId = map.get("supervisorContactId");
        if(null != supervisorContactId){
            int count = 0;
            SystemAuthenticationList systemAuthenticationList = iSystemAuthenticationListService.selectSystemAuthenticationListByContactId(supervisorContactId.toString());
            count = count + iSystemAuthenticationListService.deleteSystemAuthenticationList(systemAuthenticationList);
            FrontLineSupervisorDepts frontLineSupervisorDepts = iFrontLineSupervisorDeptsService.selectFrontLineSupervisorDeptsBySID(supervisorContactId.toString());
            count = count + iFrontLineSupervisorDeptsService.deleteFrontLineSupervisorDepts(frontLineSupervisorDepts);
            if(count == 2){
                ContactBasic contactBasic = iContactBasicService.selectContactBasicByContactID(supervisorContactId.toString());
                logUtil.insertSystemEventLog(staffName.toString(), new Date(), "删除了" + contactBasic.getStaffName() + "的管理范围和权限", "系统");
                resultEntity.setCode("S");
                resultEntity.setMsg("删除成功");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("删除失败");
        return resultEntity;
    }
}
